package com.NeuglsWorkStudio.UI
{
	import com.NeuglsWorkStudio.UI.Buttons.TTipButton;
	import com.NeuglsWorkStudio.UI.Buttons.TTipButtonEx;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	
	/**这个是很多个按钮的集合
	 * 注册点在中心
	 * @author Neugls**/
	public class Actions extends Sprite
	{
		
		private var _FButtons:Vector.<TTipButtonEx>;
		private const ButtonCount:int=7;
		public static var  ElementMargin:int=15;
		private var _FButtonImageArray:Array=null;
		private var _FWidth:Number;
		private var _FTotalWidth:Number;
		
		private  var _FTipBackgroundColor:int=0x222222;
		
		public function Actions(ButtonConfigArray:Array)
		{
			super();
			
			_FButtonImageArray=ButtonConfigArray;
			Init();
			
			Paint();
		}
		
		public function get TotalWidth():Number
		{
			return _FTotalWidth;
		}

		public function get Width():Number
		{
			return _FWidth;
		}

		public function set Width(value:Number):void
		{
			_FWidth = value;
			
			var t:Number=_FTotalWidth-_FWidth;
			var I:int=0;
			for(var i:int=0;i<_FButtons.length;i++){
				this.SetButtonVisible(i,true,false);
			}
			while(t>0&&I<_FButtons.length){
				this.SetButtonVisible(I,false,false);
				t-=_FButtons[I].width+ElementMargin;
				I++;
			}
			Paint();
		}

		public function get TipBackgroundColor():int
		{
			return _FTipBackgroundColor;
		}

		public function set TipBackgroundColor(value:int):void
		{
			_FTipBackgroundColor = value;
			Paint();
		}
		
		public function SetButtonVisible(ID:int,Visible:Boolean,CanPaint:Boolean=true):void{
			if(ID<-1||ID>_FButtons.length-1)
				return ;
			_FButtons[ID].visible=Visible;
			if(CanPaint){
				Paint();
			}
		}
		
		public function SetButtonsVisible(IDS:Array,Visible:Boolean):void{
			for each(var I:int in IDS){
				SetButtonVisible(I,Visible,false);
			}
			Paint();
		}
		
		public function GetButtonVisible(ID:int):Boolean{
			if(ID<-1||ID>_FButtons.length-1)
				return false;
			return _FButtons[ID].visible;
		}

		protected function Init():void{
			_FButtons=new Vector.<TTipButtonEx>();
			_FTotalWidth=0;
			for(var i:int=0;i<_FButtonImageArray.length;i++){
				_FButtons.push(new TTipButtonEx('',
					(_FButtonImageArray[i].Image as DisplayObject).width+15,
					(_FButtonImageArray[i].Image as DisplayObject).height+15,false,
					TipBackgroundColor
				));
				_FButtons[i].Image=_FButtonImageArray[i].Image;
				_FButtons[i].Tip=_FButtonImageArray[i].Tip;
				_FButtons[i].TipBackgroundColor=TipBackgroundColor;
				_FButtons[i].AutoAdjustColor=true;
				_FButtons[i].name=_FButtonImageArray[i].name;
				if(_FButtonImageArray[i].MouseDown){
					_FButtons[i].MouseDownImg=_FButtonImageArray[i].MouseDown;
				}
				if(_FButtonImageArray[i].MouseOver){
					_FButtons[i].MouseOverImg=_FButtonImageArray[i].MouseOver;
				}
				if(_FButtonImageArray[i].Disable){
					_FButtons[i].DisabledImg=_FButtonImageArray[i].Disable;
				}
				
				
				var FF:Function=function(a:Object,S:TTipButton,A:Boolean):void{
					if(a.OnClickParam){
						if(A){
							if(a.OnClick2){
								a.OnClick2(S);
							}
						}else{
							if(a.OnClick){
								a.OnClick(S);
							}
						}
					}else{
						if(A){
							if(a.OnClick2){
								a.OnClick2();
							}
						}else{
							if(a.OnClick){
								a.OnClick();
							}
						}
					}
				}
				
				if(_FButtonImageArray[i].DoubleState){
					_FButtons[i].DoubleStateButton=true;
					_FButtons[i].Image2=_FButtonImageArray[i].Image2;
					_FButtons[i].Tip2=_FButtonImageArray[i].Tip2;
					
					if(_FButtonImageArray[i].MouseDown2){
						_FButtons[i].MouseDownImg2=_FButtonImageArray[i].MouseDown2;
					}
					if(_FButtonImageArray[i].MouseOver2){
						_FButtons[i].MouseOverImg2=_FButtonImageArray[i].MouseOver2;
					}
					if(_FButtonImageArray[i].Disable2){
						_FButtons[i].DisabledImg2=_FButtonImageArray[i].Disable2;
					}
					_FButtons[i].OnClick=ReturnFunction(_FButtonImageArray[i],_FButtons[i],FF);
					_FButtons[i].OnClick2=ReturnFunction(_FButtonImageArray[i],_FButtons[i],FF,true);
					
				}else{
					if(_FButtonImageArray[i].OnClickParam){
						_FButtons[i].OnClick=ReturnFunction(_FButtonImageArray[i],_FButtons[i],FF);
					}else{
						_FButtons[i].OnClick=_FButtonImageArray[i].OnClick;
					}
				}
				this.addChild(_FButtons[i]);
				_FTotalWidth+=_FButtons[i].width;
			}
			_FTotalWidth+=(_FButtons.length-1)*ElementMargin;
		}
		
		private function ReturnFunction(O:Object,S:TTipButton,F:Function,A:Boolean=false):Function{
			return function():void{
				F(O,S,A);
			}
		}
		
		protected function Paint():void{
			var lastX:int=0;
			_FWidth=0;
			for(var i:int=0;i<_FButtons.length;i++){
				if(_FButtons[i].visible){
					_FButtons[i].x=lastX;
					_FButtons[i].TipBackgroundColor=_FTipBackgroundColor;
					_FButtons[i].BaseColor=_FTipBackgroundColor;
					_FWidth=lastX+_FButtons[i].width;
					lastX+=_FButtons[i].width+ElementMargin;
				}
			}
		}
	}
}